<template>
	<view class="content">
		<!-- <view class="item" v-if="type==1 &&!isback">
			<p>原密码</p>
			<p><input class="uni-input" type="safe-password" password="true" placeholder="请输入原密码" v-model="oldPassword"
					placeholder-class="input_p" /></p>
		</view> -->
		<view class="cent">
			<!-- <view class="item" v-if="isback">
				<p>手机号</p>
				<p>{{phone}}</p>
			</view> -->
			<view class="item">
				<image src="../../static/img/forgot-01.png" mode="widthFix"></image>
				<p>手机号码</p>
				<p>
					<input class="uni-input" placeholder="请输入手机号码" v-model="phone" placeholder-class="input_p" />
				</p>
			</view>
			<view class="item">
				<image src="../../static/img/forgot-01.png" mode="widthFix"></image>
				<p>手机验证码</p>
				<p>
					<input class="uni-input" placeholder="" v-model="code" placeholder-class="input_p" />
				</p>
				<text v-if="!istime" class="get get_btn" @click="get()" id="code_text">获取验证码</text>
				<text v-if="istime" class="get" id="code_text">{{countdown}}s</text>

			</view>
			<view class="item">
				<image src="../../static/img/sign-info-02.png" mode="widthFix"></image>
				<p>新密码</p>
				<p><input class="uni-input" type="safe-password" password="true" placeholder="请输入新密码"
						v-model="newPassword" placeholder-class="input_p" /></p>
			</view>
			<view class="item">
				<image src="../../static/img/sign-info-02.png" mode="widthFix"></image>
				<p>确认新密码</p>
				<p><input class="uni-input" type="safe-password" password="true" placeholder="请再次输入新密码"
						v-model="ageinPassword" placeholder-class="input_p" /></p>
			</view>

			<view class="btn" @click="backPassword">确定找回</view>
			<!-- <view class="btn" v-if="type==1&&isback" @click="backPassword">确定找回</view> -->
			<!-- <view class="btn" v-else @click="save">重设密码</view> -->
			<!-- <view class="hint" @click="getback" v-if="type==1">{{isback ? '修改密码' :'找回密码'}}</view> -->
		</view>
	</view>
</template>

<script>
	import {
		// updatePassword,
		// updateAccountPassword,
		// sendUpdatePasswordCode,
		// getBackPassword,
		userCode,
		getBack
	} from '@/common/js/index.js'
	export default {
		data() {
			return {
				oldPassword: "",
				newPassword: '',
				ageinPassword: '',
				type: '1',
				isback: false,
				phone: '',
				countdown: 120,
				timer: '',
				istime: false,
				code: '',
				requestStatus: false
			}
		},
		onLoad(e) {
			// console.log(e);
			// this.type = e.type
			// this.phone = e.phone
			// if (this.type == 1) {
			// 	uni.setNavigationBarTitle({
			// 		title: '修改登录密码'
			// 	});
			// } else {
			// 	uni.setNavigationBarTitle({
			// 		title: '修改二级密码'
			// 	});
			// 	this.isback = true
			// }
		},
		methods: {
			backPassword() {
				var hint
				if (!this.newPassword) {
					hint = '请输入新密码'
				} else if (!hint && !this.ageinPassword) {
					hint = '请再次输入新密码'
				} else if (!hint && this.newPassword != this.ageinPassword) {
					hint = '两次密码不一致，请重新输入'
				}
				if (hint) {
					uni.showToast({
						title: hint,
						icon: 'none',
						duration: 3000
					})
					return
				}
				if (this.requestStatus) {
					return
				}
				this.requestStatus = true
				var that = this
				getBack({
					mobile: this.phone,
					code: this.code,
					user_pwd: this.newPassword
				}).then(res => {
					console.log(res);
					if (res.code==200) {
						uni.showToast({
							title: '找回成功，请重新登录',
							icon: 'none',
							duration: 2000
						})
						setTimeout(function() {
							that.clear()
							uni.reLaunch({
								url: '/pages/inlet/login',
							})
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
					setTimeout(function() {
						that.requestStatus = false
					}, 2000)
				})
			},
			get() {
				// var that = this
				userCode({
					mobile:this.phone
				}).then(res => {
					console.log(res);
					if (res.code==200) {
						this.istime = true
						this.timer = setInterval(() => {
							if (this.countdown > 0) {
								this.countdown--;
							} else {
								this.istime = false
								clearInterval(this.timer)
							}
						}, 1000)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 2000,
						});
					}
				})
			},
			// getback() {
			// 	this.isback = !this.isback
			// 	if (this.isback) {
			// 		uni.setNavigationBarTitle({
			// 			title: '找回登录密码'
			// 		});
			// 	} else {
			// 		uni.setNavigationBarTitle({
			// 			title: '修改登录密码'
			// 		});
			// 	}
			// },
			clear() {
				uni.setStorageSync('islogin', false)
				uni.setStorageSync('token', '')
			},
			save() {
				var hint
				if (!this.oldPassword && this.type == 1) {
					hint = '请输入原密码'
				} else if (!hint && !this.newPassword) {
					hint = '请输入新密码'
				} else if (!hint && !this.ageinPassword) {
					hint = '请再次输入新密码'
				} else if (!hint && this.newPassword != this.ageinPassword) {
					hint = '两次密码不一致，请重新输入'
				}
				if (hint) {
					uni.showToast({
						title: hint,
						icon: 'none',
						duration: 3000
					})
					return
				}
				if (this.requestStatus) {
					return
				}
				this.requestStatus = true
				var that = this
				if (this.type == 1) {
					updatePassword({
						oldPassword: this.oldPassword,
						newPassword: this.newPassword
					}).then(res => {
						if (res.success) {
							uni.showToast({
								title: '修改成功，请重新登录',
								icon: 'none',
								duration: 2000
							})
							setTimeout(function() {
								that.clear()
								uni.reLaunch({
									url: '/pages/index/index',
								})
							}, 2000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
						setTimeout(function() {
							that.requestStatus = false
						}, 2000)
					})
				} else {
					updateAccountPassword({
						code: this.code,
						newPassword: this.newPassword
					}).then(res => {
						if (res.success) {
							uni.showToast({
								title: '修改成功，请重新登录',
								icon: 'none',
								duration: 2000
							})
							setTimeout(function() {
								that.clear()
								uni.reLaunch({
									url: '/pages/index/index',
								})
							}, 2000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
						setTimeout(function() {
							that.requestStatus = false
						}, 2000)
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100vw;
		// height: calc(100vh - 20rpx);
		background-color: #F7F7F7;
		overflow: hidden;
		.cent {
			height: 80vh;
			margin: 40rpx 30rpx 0;
			background-color: white;
			border-radius: 12rpx;
		}
	}
	.item {
		height: 110rpx;
		/* margin: 0 30rpx; */
		margin: 0 30rpx;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		border-bottom: 1rpx solid #EEEEEE;

		image {
			width: 48rpx;
			margin-right: 20rpx;
		}
	}

	.item p:nth-of-type(1) {
		font-size: 28rpx;
	}

	.item p:nth-of-type(2) {
		flex: 1;
		text-align: right;
	}

	.get {
		height: 52rpx;
		line-height: 52rpx;
		padding: 0 20rpx;
		border-radius: 25rpx;
		font-size: 24rpx;
		margin-left: 20rpx;
		background-color: #FF524F;
		color: white;
	}

	input {
		text-align: right;
		font-size: 26rxp
	}

	.input_p {
		font-size: 26rpx;
		text-align: right;
	}

	.btn {
		width: 460rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient( to right, #FF5F3B 0%, #FF524F 100%);
		border-radius: 40rpx;
		color: white;
		font-size: 32rpx;
		text-align: center;
		margin: 150rpx auto 0;
	}

	.clo_9 {
		color: #999999;
	}

	.hint {
		text-align: center;
		margin-top: 30rpx;
		font-size: 26rpx;
	}
</style>